package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.NotifyListChanged;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/25.
 */

public class MyMessageModel extends ObservableWithGoBackHandler implements NotifyListChanged {
    private boolean unreaded;
    private boolean readed;
    private ListBindingAdapter messageList;
    private UserInfoModel userInfo;

    @Bindable
    public boolean isUnreaded() {
        return unreaded;
    }

    public void setUnreaded(boolean unreaded) {
        this.unreaded = unreaded;
        if (this.unreaded) {
            this.setReaded(false);
            load();
        }
        notifyPropertyChanged(BR.unreaded);
    }

    @Bindable
    public boolean isReaded() {
        return readed;
    }

    public void setReaded(boolean readed) {
        this.readed = readed;
        if (this.readed) {
            this.setUnreaded(false);
            load();
        }
        notifyPropertyChanged(BR.readed);
    }

    @Bindable
    public ListBindingAdapter getMessageList() {
        return messageList;
    }

    public MyMessageModel(Context context, UserInfoModel userInfo) {
        super(context);
        this.unreaded = true;
        this.userInfo = userInfo;
        super.getHandler().setTitle("我的消息");
        messageList = new ListBindingAdapter<MessageItem>(getContext(), BR.message);
        this.load();
    }

    public void load() {
        final int type = readed ? 1 : 0;
        final Context context = getContext();
        messageList.clear(true);
        LoadingDialog.getInstance(context).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                ArrayList list = WebApi.getMyNews(context, type);
                try {
                    for (Object message : list) {
                        LinkedTreeMap map = (LinkedTreeMap) message;
                        MessageItem item = new MessageItem(context);

                        item.setTitle((String) map.get("title"));
                        item.setContent((String) map.get("context"));
                        item.setId(Utils.getIntValue(map.get("id")));
                        item.setMsgtime(Utils.getDateFromFormat(map.get("msgtime"), "yyyy-MM-dd HH:mm:ss"));

                        messageList.add(item);
                    }
                    if (unreaded) {
                        userInfo.setWdxx(list.size());
                    }
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                notifyDataSetChanged(messageList);
                LoadingDialog.getInstance(context).dismiss();
            }
        }).start();
    }

    @Override
    public void notifyListChanged(int action, BindingListItem item) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                load();
            }
        });
    }
}
